/*
 ** writen by hanv
 ** n2.vietha@gmail.com
 */
package vn.vng.research.fsd.data;

import java.util.HashMap;
import java.util.LinkedList;

import vn.vng.research.fsd.Configuration;

public class SearchCache {
	private HashMap<Long, SearchResult> cache;
	private int limit;
	private LinkedList<Long> queue;

	public SearchCache() {
		cache = new HashMap<Long, SearchResult>();
		queue = new LinkedList<Long>();
		limit = Configuration.loadSearchCacheLimit();
	}

	public SearchCache(SearchCache cache) {
		this.cache = cache.getCache();
		this.queue = cache.getQueue();
		this.limit = cache.getLimit();
	}
	
	public void put(SearchResult searchResult){
		cache.put(searchResult.getId(), searchResult);
		queue.offer(searchResult.getId());
		
		while (queue.size()>limit){
			cache.remove(queue.poll());
		}
	}
	
	public SearchResult get(long id){
		return cache.get(id);
	}
	
	public void clear(){
		cache.clear();
		queue.clear();
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public HashMap<Long, SearchResult> getCache() {
		return cache;
	}

	public LinkedList<Long> getQueue() {
		return queue;
	}
	
	
}
